Ext.ns('TaxonTagger');
TaxonTagger.parseName = function(config){
	
	Ext.apply(this, config, {
			width: 350
		,	height: 240
		,	layout: 'fit'
		,	modal: true
		,	resizable: false
		,	defaults: {
					border: false
				,	xtype: 'button'	
			}
		,	plain: true
		,	closable: true	
		,	fbar: {
					xtype: 'toolbar'
				,	items: [{
							text: 'Accept Name'
						,	scope: this 
						,	ref: '../refAcceptName'	
						,	handler: function(){
								var sciname = Ext.get('div-parse').select('div.div-canonical').elements[0].attributes.getNamedItem('tmpVal').value || this.parsename;
								var authorship = Ext.get('div-parse').select('div.div-authorship').elements[0].attributes.getNamedItem('tmpVal').value || this.parsename;
								var genus = Ext.get('div-parse').select('div.div-genus').elements[0].attributes.getNamedItem('tmpVal').value || '';
								TaxonTagger.currentDocument.customWords.push(this.parsename);
								var b = Ext.getCmp('mifp').getFrameWindow().document.body;
								var rg = new RegExp(this.parsename, "ig");
								b.innerHTML = b.innerHTML.replace(rg, "<span class=\"tt-sciname manual authorship\" type='Manual' scientificname='" + sciname + "'verbatim='" + this.parsename + "' authorship='"+authorship+"'offset='-1'>" + this.parsename + "</span>" +
									"<span class=\"tt-remove manual authorship\" title=\"This removes the scientific name from the list.\">&nbsp;&nbsp;</span>");
								Ext.each(TaxonTagger.currentDocument.removedWords, function(item){
									if(item == this.parsename){
										TaxonTagger.currentDocument.removedWords.remove(item);
									}
								},this);
								Ext.getCmp('mifp').generateList();
								var index = TaxonTagger.docStore.findExact('scientificName', sciname);
								var rec = TaxonTagger.docStore.getAt(index)
								rec.set('genus', genus);
								rec.commit();
								TaxonTagger.docStore.commitChanges();
								Ext.getCmp('mifp').fireEvent('docDataChanged', this);
								this.close();
							}		 
          }, {
							text: 'Close'
						,	scope: this
						,	handler: function(){
								this.close();
							}	
          }]
      }
		,	listeners: {
				render: this.callbackParse
			} 	
		,	parseNameTempalte: new Ext.XTemplate(
					'<div id= "div-parse" class="main">'
				,	'<tpl for=".">'
			//	,	'<tpl if!="original"><div class="parse-div"><b>Original: </b>{original}</div></tpl>'
				,	'<tpl if!="type"><div class="parse-div"><b>Type: </b>{type}</div></tpl>'
				,	'<tpl if!="standard"><div class="parse-div"><b>Standard: </b>{standard}</div></tpl>'
				,	'<tpl if!="canonical"><div class="div-canonical parse-div" tmpVal="{canonical}"><b>Canonical: </b>{canonical}</div></tpl>'
				,	'<tpl if!="genus"><div class="div-genus parse-div" tmpVal="{genus}"><b>Genus: </b>{genus}</div></tpl>'
				,	'<tpl if!="specific"><div class="parse-div"><b>Specific: </b>{specific}</div></tpl>'
				,	'<tpl if!="infraSpecific"><div class="parse-div"><b>InfraSpecific: </b>{infraSpecific}</div></tpl>'
				,	'<tpl if!="rank"><div class="parse-div"><b>Rank: </b>{rank}</div></tpl>'
				,	'<tpl if!="authorship"><div class="div-authorship parse-div" tmpVal="{authorship}"><b>Authorship: </b>{authorship}</div></tpl>'
				,	'<tpl if!="bracketAuthorship"><div class="parse-div"><b>Bracket Authorship: </b>{bracketAuthorship}</div></tpl>'
				,	'</tpl>'
				,	'</div>'
			)
	});
	
	TaxonTagger.parseName.superclass.constructor.call(this, config);
	
};

Ext.extend(TaxonTagger.parseName, Ext.Window, {

	callbackParse: function(){
		this.refAcceptName.disable();
		Ext.ux.JSONP.request('http://ecat-dev.gbif.org/ws/parser', {
				callbackKey: 'callback'
			,	scope:this	
			,	params: {
					names: this.parsename
				}	
			,	callback: function(response){
					this.tploutput = '';
					this.tploutput = this.parseNameTempalte.apply(response.data);
					this.body.update(this.tploutput);
					this.refAcceptName.enable();
				}
		}, this);		
	}

});